package com.glodon.paas.databag.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glodon.paas.databag.service.DatabagService;
import com.glodon.paas.exception.PaasServerExceptionFactory;

/**
 * 处理所有文件相关的资源.
 * 
 * @author botao.liao
 * 
 */
@Controller
@RequestMapping("/rvt")
public class RevitController {

    @Autowired
    private DatabagService databagService;

    @RequestMapping(value = "/{databagId}/{constructionId}", method = RequestMethod.GET)
    public void getConstrunction(@PathVariable("databagId") String databagId,
            @PathVariable("constructionId") String constructionId, HttpServletResponse resp) throws IOException {
        String construction = databagService.getRevitConstruction(databagId, constructionId);
        if (construction == null) {
            throw PaasServerExceptionFactory.notFoundException("not found construction " + constructionId
                    + " in databag " + databagId, null);
        }
        resp.addHeader("Cache-Control", "max-age=315360000000");
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(construction);
        resp.flushBuffer();
    }
}
